﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Edu.Application.Model.RequestModel;
using EStart.Infrastructure.ClientData;
using Edu.Application.Implements;
using Edu.Application.Interfaces;
using Edu.Api.Authentication;
using EStart.Infrastructure.Domain;
using Edu.Application.Model.Messaging;
using Edu.Application.Model.Models;
using Edu.Application.Model.ResponseModel;
using EStart.Infrastructure.Caching;

namespace Edu.Api.Controllers
{
    [Produces("application/json")]
    [Route("api/account")]
    public class AccountController : APIController
    {
        private readonly IUserService _service;

        public AccountController(IUserService service)
        {
            _service = service;
        }
        // 登录
        [HttpPost("login")]
        public async Task<ApiResult<LoginResponse>> Login([FromBody]LoginRequest request)
        {
            var response = await _service.LoginAsync(request);
            if (response.success)
            {
                var token = GetToken(response.data.Guid);
                response.data.SignToken = token.SignToken;
            }
            return response;
        }
        // 登出
        [HttpPost("logout")]
        public ApiResult<string> Logout([FromBody]LogoutReqeust reqeust)
        {
            var result = new ApiResult<string>();
            try
            {
                ClearToken(reqeust.Guid);
                result.message = "注销成功.";
                result.statusCode = 200;
                result.success = true;
            }
            catch (Exception ex)
            {
                result.statusCode = (int)StatusCodeEnum.Error;
                result.message = StatusCodeEnum.Error.GetEnumText()+":"+ex.Message;
            }
            return result;
        }
    }
}